package com.syj.jsxjj.config;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.syj.jsxjj.ui.my.bean.UserInfoBean;
import com.syj.jsxjj.util.ConvertUtil;
import com.syj.jsxjj.util.SpUtil;

import java.util.ArrayList;
import java.util.List;

public class ConfigUtil {
    private boolean isDebug = true;//是否调试模式,上线必须改为false

    public String baseUrl = "http://121.43.57.157:84/";
    public String[] urls = {
            "http://api.souyijie.cn",
            "http://www.bugtomoney.com:84",
            "http://121.43.57.157:84"
    };
    //爬取支付宝数据js
    public String GET_ALIPAY_JS = baseUrl + "resources/js/alipay.js";

    //我的邀请码H5
    public String INVITATION_CODE = baseUrl + "page/detail";

    //消息中心H5
    public String ACTIVITY_CENTER = baseUrl + "content/activity";

    //消息中心详情H5
    public String ACTIVITY_DETAILS = baseUrl + "content/details?id=37";

    //帮助中心H5
    public String HELP = baseUrl + "help";

    //发现H5
    public String DISCOVER = baseUrl + "credit-card/find";

    //发现-办卡进度H5
    public String CARDPROGRESS = baseUrl + "credit-card/findSchedule";

    //注册协议
    public String REGISTER_AGREEMENT = baseUrl + "act/light-loan-xjx/agreement";

    //信用授权协议
    public String CREDIT_AUTHORIZATION_AGREEMENT = baseUrl + "agreement/creditExtension";

    //关于我们
    public String ABOUT_US = baseUrl + "page/detailAbout";

    /*优惠券解答接口solve/coupon
    芝麻认证solve/zm
    紧急联系人solve/contact
    手机运营商solve/phone
    个人信息认证solve/register*/
    public String SOLVE_COUPON = baseUrl + "solve/coupon";
    public String SOLVE_ZM = baseUrl + "solve/zm";
    public String SOLVE_CONTACT = baseUrl + "solve/contact";
    public String SOLVE_PHONE = baseUrl + "solve/phone";
    public String SOLVE_REGISTER = baseUrl + "solve/register";

    private boolean isLogin = false;//用户的登陆状态

    private String channelName = "xjx-MySelf";//默认渠道号

    private List<UserInfoBean> users;
    public UserInfoBean userInfo;
    public ConfigUtil() {
        setUserInfo(getUserInfo());
    }

    public boolean isDebug() {
        return isDebug;
    }

    public void setDebug(boolean isDebug) {
        this.isDebug = isDebug;
    }


    public String getChannelName() {
        return channelName;
    }


    public void setChannelName(String channelName) {
        this.channelName = channelName;
    }

    public UserInfoBean getUserInfo() {
        if (userInfo==null){
            userInfo = ConvertUtil.toObject(SpUtil.getString(Constant.CACHE_USER_INFO), UserInfoBean.class);
        }
        return userInfo;
    }


    public void setUserInfo(UserInfoBean userInfo) {
        isLogin = userInfo == null ? false : true;
        this.userInfo = userInfo;
        SpUtil.putString(Constant.CACHE_USER_INFO, ConvertUtil.toJsonString(userInfo));
    }

    public List<UserInfoBean> getUsers() {
        List<UserInfoBean> users = new Gson().fromJson(SpUtil.getString(Constant.CACHE_USER_INFOS), new TypeToken<List<UserInfoBean>>() {
        }.getType());
        if (users==null){
            users = new ArrayList<>();
        }
        return users;
    }


    public void setUsers(List<UserInfoBean> users) {
        String s2 = new Gson().toJson(users);
        Log.e("uses",s2);
        SpUtil.putString(Constant.CACHE_USER_INFOS, s2);
    }

    //获取用户当前登录状态
    public boolean getLoginStatus() {
        return isLogin;
    }

    public String getBaseUrl() {
        if (isDebug() && !TextUtils.isEmpty(SpUtil.getString(Constant.URL_KEY))) {
            baseUrl = SpUtil.getString(Constant.URL_KEY);
        }
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
        GET_ALIPAY_JS = baseUrl + "resources/js/alipay.js";

        //我的邀请码H5
        INVITATION_CODE = baseUrl + "page/detail";

        //消息中心H5
        ACTIVITY_CENTER = baseUrl + "content/activity";

        //消息中心详情H5
        ACTIVITY_DETAILS = baseUrl + "content/details?id=37";

        //帮助中心H5
        HELP = baseUrl + "help";

        //注册协议
        REGISTER_AGREEMENT = baseUrl + "act/light-loan-xjx/agreement";

        //关于我们
        ABOUT_US = baseUrl + "page/detailAbout";

        //信用授权协议
        CREDIT_AUTHORIZATION_AGREEMENT = baseUrl + "agreement/creditExtension";

        //发现
        DISCOVER = baseUrl + "credit-card/find";

        //发现-办卡进度H5
        CARDPROGRESS = baseUrl + "credit-card/findSchedule";

        SOLVE_COUPON = baseUrl + "solve/coupon";
        SOLVE_ZM = baseUrl + "solve/zm";
        SOLVE_CONTACT = baseUrl + "solve/contact";
        SOLVE_PHONE = baseUrl + "solve/phone";
        SOLVE_REGISTER = baseUrl + "solve/register";
    }
}
